<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\User;
use app\index\model\Sendmessage;
use app\index\model\Weibo;
use think\Cookie;
use \think\Session;
class login extends Controller
{
    public function qq(Request $request){
          $code = $request->param('code');
  	 $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101450566&client_secret=f7c3c62f6b2b6d155c94a715b22e95a1&code=".$code."&redirect_uri=http://wang.myzhuke.top/old/TP5/public/index.php/index/login/qq";

	$obj= file_get_contents($url);
	$arr = explode('&', $obj);

foreach ($arr as $key => $value) {
	$data[explode('=', $value)[0]] = explode('=', $value)[1];
}
 
       $urlone='https://graph.qq.com/oauth2.0/token?grant_type=refresh_token&client_id=101450566&client_secret=f7c3c62f6b2b6d155c94a715b22e95a1&refresh_token='.$data['refresh_token'];
 
	$objone= file_get_contents($urlone);
	$arrone = explode('&', $objone);

foreach ($arrone as $key => $value) {
	$dataone[explode('=', $value)[0]] = explode('=', $value)[1];
}

      $urltwo = "https://graph.qq.com/oauth2.0/me?access_token=".$dataone['access_token'];
      
      $objtwo = file_get_contents($urltwo);
      $objtwo = explode(':',$objtwo);
      $oppid = trim(substr($objtwo[2],1,-6));
      $urlthree = "https://graph.qq.com/user/get_user_info?access_token=".$dataone['access_token']."&oauth_consumer_key=101450566&openid=".$oppid;
      $userInfoObj = file_get_contents($urlthree);  
      $userInfo = json_decode($userInfoObj,true);

      $res = Db::table('qq')->where('nickname',$userInfo['nickname'])->find();
      if(!$res){
         $qid = Db::name('qq')->insertGetId(['nickname'=>$userInfo['nickname'],'gender'=>$userInfo['gender'],'year'=>$userInfo['year'],'figureurl_qq_1'=>$userInfo['figureurl_qq_1']]);
  
         $ress = Db::table('user')->where('tid',$qid)->find();
 
         if(!$ress){
           header("location:http://wang.myzhuke.top/old/Index1/login.html?status=204&tid=".$qid);die;
          }else{
           header("location:http://wang.myzhuke.top/old/Index1/login.html?status=200&userId=".$ress['userId']);die; 
          }
      }else{
         $ress = Db::table('user')->where('tid',$res['qid'])->find();
     
         if(!$ress){
           header("location:http://wang.myzhuke.top/old/Index1/login.html?status=204&tid=".$res['qid']);die;
          }else{
           header("location:http://wang.myzhuke.top/old/Index1/login.html?status=200&userId=".$ress['userId']);die; 
          }
      }
      
}
    //新浪微博的第三方登录
    public function weibo(Request $request){
      // Cookie::set('name','value',3600);
      // echo Cookie::get('name');
       $code = $request->param('code');
       $callback = $request->param("callback");

       $obj = new Weibo;
       $userInfo = $obj->get_token($code);
       //将第三方接口的信息入库
       $userInfo['add_time'] = time();
       // var_dump($userInfo);die;

       $threeId = $obj->add($userInfo);

       //根据第三方id关联用户表
       $user = new User;
       $threeInfo = $user->show('null','null',$threeId);
       if($threeInfo){
           header("location:http://47.93.21.169/old/Index/login.html?status=200&userId=".$threeInfo['userId']);die;

       }else{
           header("location:http://47.93.21.169/old/Index/login.html?status=204&tid=$threeId");die;
       }

    }

    //第三方id绑定用户账号
    public function bindId(Request $request){
        $param = $request->param();

//        $callback  = $request->param('callback');
        $user = new User;
        $bool = $user->gai($param);

        if($bool == 'nullArr'){
            $arr['status'] = 201;
            $arr['msg'] ='还没有用户注册账号，请先注册';
        }elseif($bool=='null'){
            $arr['status'] = 202;
            $arr['msg'] ='你输入的账号不存在,请重新绑定';
        }else{
            $arr['status'] = 200;
            $arr['msg'] ='绑定用户账号成功';
            $arr['data']=$bool;
        }
        echo callback($param['callback'],$arr);
    }

    //用户登录功能实现
    public function index(Request $request)
    {

       $username = $request->param('username');
       $userpwd  = md5($request->param('userpwd'));
       $callback  = $request->param('callback');
       $obj = new User;
       $user = $obj->show($username,$userpwd);
      // var_dump($user);die;
       if($user)
       {
          $arr['status'] = 200;
          $arr['msg'] = '成功';
          $arr['userId'] = $user['userId'];
          $arr['account'] = $user['account'];
       }
       else
       {
          $arr['status'] = 204;
          $arr['msg'] = '失败';
       }

       echo callback($callback,$arr);
           
    }

    //用户注册功能实现
    public function register(Request $request)
    {
       $param= $request->param();
       $param['addTime'] = time();
       // var_dump($param);die;

       $obj = new User;
       //先查询在插入
       $res = $obj->show($param['account']);
       if(!$res){
           $bool = $obj->add($param);
           if($bool){
              $arr['status'] = 200;
              $arr['msg'] = '成功';
           }else{
              $arr['status'] = 400;
              $arr['msg'] = '失败';
           }
       }else{
              $arr['status'] = 100;
              $arr['msg'] = '该用户已经被注册';
       }

       echo callback($param['callback'],$arr);


    }

  /*
  * 发送短信接口
  */
  public function send(Request $request)
  {
    //实例化model
    $model = new Sendmessage;
    //接收手机号
    $mobile = $request->param("realMobile");
    $callback = $request->param("callback");

    //$mobile = '17601624430';
    //验证码
    $code = $model->createCode(4,1);

    Cookie::set('code',$code);
    $post_data = "account=C02343298&password=e20ed35afd656249ca645db630cc47d3&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$code."。请不要把验证码泄露给其他人。");
    $data = $model->sendPost($post_data);

  }

  /*
  *   验证手机验证码是否正确
  */
  public function verify(Request $request)
  {
    $code = $request->param("code");
    $callback = $request->param('callback');
    $scode = Cookie::get('code');
    if($code==$scode){
      $arr['status'] = 200;
      $arr['msg'] = '成功';
    }else{
      $arr['status'] = 204;
      $arr['msg'] = '验证码不正确';
    }
    // echo json_encode($arr);
    echo callback($callback,$arr);
  }

    //验证验证码
    public function captcha(Request $request){
     // return 111;die;
      $authCode = $request->param('authCode');
      $callback = $request->param('callback');
      // return $authCode;die;
      $captcha = new \think\captcha\Captcha();
      // return $captcha;die;
      if (!$captcha->check($authCode)) {
           $arr['status'] = 0;
      }else{
           $arr['status'] = 1;
      }
      echo callback($callback,$arr);

    }
}
